import {
	warning,
	loading,
	error
} from "./toast.js"

let urlConfig='https://api.sytcqc.com'

const uniRequest = (opt) => {
	opt = opt || {}
	opt.url = opt.url || ""
	opt.data = opt.data || null
	opt.method = opt.method || "GET"
	opt.header = opt.header || {
		"Content-Type": "application/json",
	}
	const token = uni.getStorageSync("token")
	if (token) {
		// 存在tonken就存入header
		//console.log("拿到了token")
		opt.header = {
			...opt.header,
			token: token,
		}
	} else {
		//console.log("不能获取到token")
	}

	opt.success = opt.success || function() {}
	if(opt.url!='/books/exhibition'){
		loading()
	}
	uni.request({
		url: urlConfig + opt.url,
		data: opt.data,
		method: opt.method,
		header: opt.header,
		dataType: "json",
		success: function(res) {
			uni.hideToast()
			uni.hideLoading()
			// uni.hideToast();
			  // 延时关闭 加载中的 loading框
				        // setTimeout(()=>{
				        //     uni.hideLoading()
				        // },2000)
			if (res.data.code == 200) {
				uni.hideToast()
				uni.hideLoading()
				opt.success(res.data)
			} else {
				error(res.data.msg)
				opt.success(res.data)
			}
		},
		fail: function(err) {
			//console.log(err)
			warning("请稍后重试")
		},
	})
}

export {
	uniRequest,
	urlConfig
}
